package com.tomtop.core.redis;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class  ByteRedisTemplate extends RedisTemplate<String, byte[]>{
	public ByteRedisTemplate() {
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		RedisSerializer<byte[]> byteSerializer = new RedisSerializer<byte[]>() {
			public byte[] deserialize(byte[] bytes) { return bytes; }
			public byte[] serialize(byte[] bytes) { return bytes; }
		};
		setKeySerializer(stringSerializer);
		setValueSerializer(byteSerializer);
		setHashKeySerializer(stringSerializer);
		setHashValueSerializer(byteSerializer);
	}
	public ByteRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}
